package com.medicine.hbp.fragment;

import java.util.List;

import org.kymjs.kjframe.http.HttpParams;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.medicine.hbp.adapter.CareforOneAdapter;
import com.medicine.hbp.adapter.CareforTwoAdapter;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.AttInfoVo;
import com.medicine.hbp.mob.FansInfoVo;
import com.medicine.hbp.mob.UserAttentionVo;
import com.medicine.hbp.mob.UserFansVo;
import com.medicine.hbp.sp.LoginSp;
import com.medicine.hbp.uitl.CommUtil;
import com.medicine.hbp.uitl.StartIntent;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.xListView.XListView;
import com.medicine.hbp.xListView.XListView.IXListViewListener;
import com.medicine.hbp.R;

/**
 * 关注
 *
 * @author Administrator
 */
public class CareforFragment extends Fragment implements OnClickListener,
		IXListViewListener {
	private LinearLayout ll_fensi, ll_guanzhu;
	private ImageView img_guanzhu, img_fensi;
	private View View;
	private XListView listViewOne, listViewTwo;
	private CareforOneAdapter adapterOne;// 关注的adapter
	private CareforTwoAdapter adapterTwo;// 粉丝的adapter
	private TextView text_guanzhu_num, text_fensi_num;

	private List<AttInfoVo> attInfoVosOne;
	private List<FansInfoVo> fansInfoVosTwo;

	private volatile boolean refreshOne = true;
	private volatile int pageOne = 1;// 当前页（从0开始）
	private volatile boolean refreshTwo = true;
	private volatile int pageTwo = 1;// 当前页（从0开始）

	private int pageSize = 10;
	private int type = 1;// 1 关注 2粉丝

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.arg2 == 0) {
				attInfoVosOne = adapterOne.getItems();
				for (int i = 0; i < attInfoVosOne.size(); i++) {
					if (msg.arg1 == attInfoVosOne.get(i).userId) {
						attInfoVosOne.remove(i);
						adapterOne.setItems(attInfoVosOne);
						adapterOne.notifyDataSetChanged();
						return;
					}

				}
			} else if (msg.arg2 == 1) {
				fansInfoVosTwo = adapterTwo.getItems();
				for (int i = 0; i < fansInfoVosTwo.size(); i++) {
					if (msg.arg1 == fansInfoVosTwo.get(i).userId) {
						fansInfoVosTwo.get(i).userIsFocus = true;
						adapterOne.setItems(fansInfoVosTwo);
						adapterOne.notifyDataSetChanged();
						return;
					}
				}
			}
		}
	};

	@Override
	public android.view.View onCreateView(LayoutInflater inflater,
										  ViewGroup container, Bundle savedInstanceState) {
		View = inflater.inflate(R.layout.fragment_carefor, container, false);
		ll_fensi = (LinearLayout) View.findViewById(R.id.ll_fensi);
		img_fensi = (ImageView) View.findViewById(R.id.img_fensi);
		ll_guanzhu = (LinearLayout) View.findViewById(R.id.ll_guanzhu);
		img_guanzhu = (ImageView) View.findViewById(R.id.img_guanzhu);
		listViewOne = (XListView) View.findViewById(R.id.xListView_care_one);
		listViewTwo = (XListView) View.findViewById(R.id.xListView_care_two);
		text_guanzhu_num = (TextView) View.findViewById(R.id.text_guanzhu_num);
		text_fensi_num = (TextView) View.findViewById(R.id.text_fensi_num);
		ll_fensi.setOnClickListener(this);
		ll_guanzhu.setOnClickListener(this);
		initView();
		return View;
	}

	private void initView() {
		adapterOne = new CareforOneAdapter(getActivity(), handler);
		listViewOne.setAdapter(adapterOne);
		listViewOne.setXListViewListener(this);
		adapterTwo = new CareforTwoAdapter(getActivity(), handler);
		listViewTwo.setAdapter(adapterTwo);
		listViewTwo.setXListViewListener(this);
	}

	@Override
	public void onResume() {
		super.onResume();
		if (!TextUtils.isEmpty(LoginSp.getToken(getActivity()))) {
			httpUserAttention();
			httpUserFans();
		}
	}

	@Override
	public void onClick(android.view.View v) {
		switch (v.getId()) {
			case R.id.ll_fensi:// 粉丝按钮
				type = 2;
				setImage(R.mipmap.guanzhu_01, R.mipmap.guanzhu_02);
				listViewOne.setVisibility(View.GONE);
				listViewTwo.setVisibility(View.VISIBLE);
				break;
			case R.id.ll_guanzhu:// 关注按钮
				type = 1;
				setImage(R.mipmap.guanzhu_02, R.mipmap.guanzhu_01);
				listViewOne.setVisibility(View.VISIBLE);
				listViewTwo.setVisibility(View.GONE);
				break;
		}

	}

	/**
	 * 按钮切换
	 *
	 * @param fensi
	 *            粉丝按钮
	 * @param guanzhu
	 *            关注按钮
	 */
	private void setImage(int fensi, int guanzhu) {
		img_fensi.setImageResource(fensi);
		img_guanzhu.setImageResource(guanzhu);
	}

	/**
	 * 用户获取自己关注的其他用户
	 */
	private void httpUserAttention() {
		Log.e("tag", "关注");
		HttpParams params = new HttpParams();
		params.put("current", pageOne);// 当前页数(不传默认为1)
		params.put("rowCount", pageSize);// 每页显示数(不传默认为10)
		AppHttps.HttpPost(getActivity(), HttpInterface.USER_ATTENTION, params,
				new AppHttpCallBack<UserAttentionVo>(UserAttentionVo.class,
						getActivity()) {
					@Override
					public void onSuccess(UserAttentionVo result) {
//						if(null!=result){
//						boolean hasMore = true;
//						text_guanzhu_num.setText(result.attInfo.size() + "");
//						if (refreshOne) {
//							adapterOne.changeData(result.attInfo);
//						} else {
//							adapterOne.addItems(result.attInfo);
//							adapterOne.notifyDataSetChanged();
//						}
//						if (result.attInfo.size() < pageSize) {
//							hasMore = false;
//						}
//						listViewOne.setPullLoadEnable(hasMore);
//						}
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(getActivity(), errorMsg);
					}

					@Override
					public void onFinish() {
						super.onFinish();
						listViewOne.setRefreshTime(CommUtil.formatDate(System
								.currentTimeMillis()));
					}
				});
	}

	/**
	 * 用户获取被关注的用户信息
	 */
	private void httpUserFans() {
		Log.e("tag", "粉丝");
		HttpParams params = new HttpParams();
		params.put("current", pageTwo);// 当前页数(不传默认为1)
		params.put("rowCount", pageSize);// 每页显示数(不传默认为10)
		AppHttps.HttpPost(
				getActivity(),
				HttpInterface.USER_FANS,
				params,
				new AppHttpCallBack<UserFansVo>(UserFansVo.class, getActivity()) {
					@Override
					public void onSuccess(UserFansVo result) {
//						boolean hasMore = true;
//						text_fensi_num.setText(result.fansInfo.size() + "");
//						if (refreshTwo) {
//							adapterTwo.changeData(result.fansInfo);
//						} else {
//							adapterTwo.addItems(result.fansInfo);
//							adapterTwo.notifyDataSetChanged();
//						}
//						if (result.fansInfo.size() < pageSize) {
//							hasMore = false;
//						}
//						listViewTwo.setPullLoadEnable(hasMore);
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(getActivity(), errorMsg);
					}

					@Override
					public void onFinish() {
						super.onFinish();
						listViewTwo.setRefreshTime(CommUtil.formatDate(System
								.currentTimeMillis()));
					}
				});
	}

	@Override
	public void onRefresh() {
		switch (type) {
			case 1:// 关注
				pageOne = 1;
				refreshOne = true;
				listViewOne.setPullLoadEnable(false);
				httpUserAttention();
				break;
			case 2:// 粉丝
				pageTwo = 1;
				refreshTwo = true;
				listViewTwo.setPullLoadEnable(false);
				httpUserFans();
				break;
		}

	}

	@Override
	public void onLoadMore() {
		switch (type) {
			case 1:// 关注
				pageOne++;
				refreshOne = false;
				httpUserAttention();
				break;
			case 2:// 粉丝
				pageTwo++;
				refreshTwo = false;
				httpUserFans();
				break;
		}
	}

}
